package com.zgw.his.emr.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.MENUINFO;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.internal.DetachedWindow;
import org.eclipse.ui.part.ViewPart;

/**
 * ViewUitl 插件视图工具类
 * 
 */
public final class ViewUtil {

	public static void activate(IWorkbenchPart part) {
		try {
			getActivePage().activate(part);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static IViewPart getView(String viewId) {
		IViewPart view = findView(viewId);
		if (view != null) {
			return view;
		} else {
			return showView(viewId);
		}
	}

	@SuppressWarnings("deprecation")
	public static IViewPart[] getActiveViews() {
		if (PlatformUI.getWorkbench() == null) {
			return null;
		} else if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() == null) {
			return null;
		} else if (PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage() == null) {
			return null;
		} else {
			return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
					.getActivePage().getViews();
		}
	}

	@SuppressWarnings("deprecation")
	public static List<IViewPart> getAllViews() {
		List<IViewPart> list = new ArrayList<IViewPart>();
		IWorkbenchPage[] pags = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getPages();
		for (IWorkbenchPage page : pags) {
			for (IViewPart part : page.getViews()) {
				list.add(part);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public static <T extends ViewPart> T getView(Class<T> viewClass) {
		try {
			String viewID = (String) RefMethod.getStaticProperty(viewClass,
					"ID");
			IViewPart view = findView(viewID);
			if (view != null) {
				return (T) view;
			} else {
				return (T) showView(viewID);
			}
		} catch (Exception e) {
			return null;
		}

	}

	@SuppressWarnings("unchecked")
	public static <T extends ViewPart> T findView(Class<T> viewClass) {
		try {
			String viewID = (String) RefMethod.getStaticProperty(viewClass,
					"ID");
			return (T) findView(viewID);
		} catch (Exception e) {
			return null;
		}
	}

	public static IViewPart findView(String viewId) {
		try {
			return getActivePage().findView(viewId);
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static IPerspectiveDescriptor getCurrentPerspective() {
		try {
			return getActivePage().getPerspective();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static String getCurrentPerspectiveId() {
		try {
			return getCurrentPerspective().getId();
		} catch (RuntimeException e) {
			return "";
		}
	}

	public static IPerspectiveDescriptor[] getOpenPerspectives() {
		try {
			return getActivePage().getOpenPerspectives();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static List<String> getOpenPerspectivesId() {
		try {
			List<String> list = new ArrayList<String>();
			for (IPerspectiveDescriptor id : getActivePage()
					.getOpenPerspectives()) {
				list.add(id.getId());
			}
			return list;
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static IPerspectiveDescriptor[] getAllPerspectives() {
		try {
			return getPerspectiveRegistry().getPerspectives();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static IWorkbenchPage getActivePage() {
		try {
			return getActiveWorkbenchWindow().getActivePage();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static IWorkbench getWorkbench() {
		try {
			return PlatformUI.getWorkbench();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static IWorkbenchWindow getActiveWorkbenchWindow() {
		try {
			return getWorkbench().getActiveWorkbenchWindow();
		} catch (RuntimeException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T extends ViewPart> T showView(Class<T> viewClass) {
		try {
			String viewID = (String) RefMethod.getStaticProperty(viewClass,
					"ID");
			return (T) showView(viewID);
		} catch (Exception e) {
			return null;
		}
	}

	public static IViewPart showView(String viewId) {
		try {
			return getActivePage().showView(viewId);
		} catch (PartInitException e) {
			return null;
		}
	}

	public static void hideView(String viewId) {
		try {
			IViewPart view = findView(viewId);
			if (view != null) {
				getActivePage().hideView(view);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("restriction")
	public static void close(String viewId) {
		try {
			IViewPart view = findView(viewId);
			if (view != null) {
				Shell shell = view.getViewSite().getShell();
				if (shell.getData() instanceof DetachedWindow) {
					view.getViewSite().getShell()
							.notifyListeners(SWT.Close, new Event());
				} else {
					view.getViewSite().getPage().hideView(view);
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	public static void closePerspective(String perspectiveId) {
		closePerspective(getWorkbench().getPerspectiveRegistry()
				.findPerspectiveWithId(perspectiveId));
	}

	public static void closePerspective(IPerspectiveDescriptor desc) {
		try {
			getActivePage().closePerspective(desc, false, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void invokeMethod(Object owner, String methodName,
			Object[] args) {
		try {
			if (owner != null) {
				RefMethod.invokeMethod(owner, methodName, args);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	public static void setActivePage(IWorkbenchPage page) {
		try {
			getActiveWorkbenchWindow().setActivePage(page);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	public static void openPersperctive(String id) {
		try {
			getActivePage().setPerspective(
					getWorkbench().getPerspectiveRegistry()
							.findPerspectiveWithId(id));
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	public static void openPersperctive(IPerspectiveDescriptor id) {
		try {
			getActivePage().setPerspective(id);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	public static void showPerspective(String perspectiveId,
			IWorkbenchWindow window) {
		try {
			getWorkbench().showPerspective(perspectiveId, window);
		} catch (WorkbenchException e) {
			e.printStackTrace();
		}
	}

	public static IPerspectiveRegistry getPerspectiveRegistry() {
		try {
			return getWorkbench().getPerspectiveRegistry();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static IPerspectiveDescriptor[] getPerspectives() {
		try {
			return getPerspectiveRegistry().getPerspectives();
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static void resetPerspective() {
		ActionFactory.RESET_PERSPECTIVE.create(getActiveWorkbenchWindow())
				.run();
	}

	public static IPerspectiveDescriptor findPerspectiveWithId(String id) {
		try {
			return getPerspectiveRegistry().findPerspectiveWithId(id);
		} catch (RuntimeException e) {
			return null;
		}
	}

	@SuppressWarnings("restriction")
	public static void setMenuBarBackground(Shell shell, Color c) {
		if (c != null && !c.isDisposed())
			setMenuBarBackground(shell, OS.CreateSolidBrush(c.handle));
	}

	@SuppressWarnings("restriction")
	public static void setMenuBarBackgroundImage(Shell shell, Image image) {
		if (image != null && !image.isDisposed())
			setMenuBarBackground(shell, OS.CreatePatternBrush(image.handle));
	}

	@SuppressWarnings("restriction")
	private static void setMenuBarBackground(Shell shell, int hBrush) {
		if (shell.getMenuBar() == null)
			return;
		MENUINFO lpcmi = new MENUINFO();
		lpcmi.cbSize = MENUINFO.sizeof;
		lpcmi.fMask = OS.MIM_BACKGROUND;
		lpcmi.hbrBack = hBrush;
		OS.SetMenuInfo(shell.getMenuBar().handle, lpcmi);
		shell.redraw();
	}

	// public static IViewPart openDetachedView(String viewID) {
	// try {
	// return detachedView.openDetachedView(viewID).getView(true);
	// } catch (RuntimeException e) {
	// return null;
	// }
	// }
	/**
	 * 关闭视图或透视图
	 */
	public static void closeViewOrPerspective(IViewPart part) {
		getActivePage().hideView(part);
		IViewPart[] parts = ViewUtil.getActiveViews();
		if (parts.length == 0)
			ViewUtil.closePerspective(ViewUtil.getCurrentPerspective());
	}

	/**
	 * 重新计算一个控件的大小
	 * 
	 * @param control
	 */
	public static void resetSize(Control control) {
		try {
			control.setSize(control.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	public static Point getTextSize(String text) {
		if (text == null)
			return new Point(0, 0);
		GC gc = new GC(Display.getDefault());
		Point size = gc.textExtent(text);
		gc.dispose();
		return size;
	}
}
